<?php

namespace app\admin\controller;

use app\admin\model\AdminLog;
use app\admin\validate\Admin;
use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Hook;
use think\Validate;

/**
 * 后台首页
 * @internal
 */
class Web extends Backend
{

    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['index'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
        $captcha = config('config')['captcha'];
        $this->assign("captcha", $captcha);
    }

    /**
     * 后台首页
     */
//    public function index()
//    {
//        //左侧菜单
//        list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
//            'dashboard' => 'hot',
//            'addon'     => ['new', 'red', 'badge'],
//            'auth/rule' => __('Menu'),
//            'general'   => ['new', 'purple'],
//        ], $this->view->site['fixedpage'],ORG_CATE_ID);
//        $action = $this->request->request('action');
//        if ($this->request->isPost()) {
//            if ($action == 'refreshmenu') {
//                $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
//            }
//        }
//        $this->view->assign('menulist', $menulist);
//        $this->view->assign('navlist', $navlist);
//        $this->view->assign('fixedmenu', $fixedmenu);
//        $this->view->assign('referermenu', $referermenu);
//        $this->view->assign('org_cate_id', ORG_CATE_ID);
//        $this->view->assign('title', __('Home'));
//        return $this->view->fetch();
//    }

    /**
     * 管理员登录
     */
    public function index()
    {
        $url = $this->request->get('url', 'index/index');
//        $url = 'index/index';
        if ($this->auth->isLogin()) {
            $this->redirect('index/index');
//            $this->success(__("You've logged in, do not login again"), $url);
        }
        if ($this->request->isPost()) {
            $mobile = $this->request->post('username');
            $code = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
                $this->error(__('手机号不正确'));
            }
            AdminLog::setTitle(__('Login'));
            $ret =  \app\common\library\Sms::check($mobile, $code, 'login');
//            $ret = true;
            if ($ret) {
                $admin = Db::name('admin')->where('org_cate_id', '>', 0)->where('mobile', $mobile)->find();
//                $admin = \app\admin\model\Admin::get(['mobile' => $mobile]);
                if(isset($admin['org_cate_id']) && $admin['org_cate_id'] > 0){
                    $status = Db::name('org_cate')->where('id', $admin['org_cate_id'])->value('status');
                    if($status == 1){
                        $this->error(__('该单位已经被禁⽤，请联系总平台管理员'));
                    }
                }
                if($admin){
                    session('admin',$admin);
                    $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $admin['username'], 'avatar' => $this->auth->avatar]);
                    $this->redirect('index/index');
                }else{
                    $this->error(__('手机号【'.$mobile.'】未注册或者不是单位平台手机号！'));
                }

            } else {
                $this->error(__('验证码不正确'));
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin()) {
            $this->redirect($url);
        }
        $background = Config::get('fastadmin.login_background');
        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
        $this->view->assign('background', $background);
        $this->view->assign('title', __('Login'));
        Hook::listen("admin_login_init", $this->request);
        return $this->view->fetch();
    }
}
